www.gusucode.com > VC++ 视频采集与压缩、音频采集、加扰和解绕-源码程序 > VC++ 视频采集与压缩、音频采集、加扰和解绕-源码程序/code/VideoNet_src/VideoNetDlg.cpp

    //Download by http://www.NewXing.com
////////////////////////////////////////////////////////////////////////////
//
//
//    Project     : VideoNet version 1.1.
//    Description : Peer to Peer Video Conferencing over the LAN.
//	  Author      :	Nagareshwar Y Talekar ( nsry2002@yahoo.co.in)
//    Date        : 15-6-2004.
//
//
//    File description : 
//    Name    : VideoNetDlg.cpp
//    Details : Main dialog class
//
//
/////////////////////////////////////////////////////////////////////////////


#include "stdafx.h"
#include "VideoNet.h"
#include "VideoNetDlg.h"
#include "Resource.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()










/////////////////////////////////////////////////////////////////////////////
// CVideoNetDlg dialog




// Global varialbes...
int count=0;
unsigned char cdata[40000];
int cbuffer_size=40000;
unsigned char rgbdata[320000];
int buffersize=320000;




// Callback function gets invoked during compression...
// It returns the compressed data byte by byte...
void OwnWriteFunction(int byte)
{
	if(count<cbuffer_size)
	cdata[count]=(unsigned char)byte;
	
	count++;

}



CVideoNetDlg::CVideoNetDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CVideoNetDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CVideoNetDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
//	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_hIcon = AfxGetApp()->LoadIcon(IDI_SK_LOGO);
}

void CVideoNetDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CVideoNetDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}





BEGIN_MESSAGE_MAP(CVideoNetDlg, CDialog)
	//{{AFX_MSG_MAP(CVideoNetDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	
	// Conference menu handlers
	ON_COMMAND(CONF_CONNECT,OnConnect)
	ON_COMMAND(CONF_DISCONNECT,OnDisconnect)
	ON_COMMAND(CONF_EXIT,OnCancel)
	
	// View menu handlers
	ON_COMMAND(VIEW_LOCAL,OnViewLocal)
	ON_COMMAND(VIEW_REMOTE,OnViewRemote)
	
	// Audio menu handlers
	ON_COMMAND(AUDIO_CONTROL,OnVolumeControl)
	ON_COMMAND(AUDIO_SEND,OnAudioSend)
	ON_COMMAND(AUDIO_RECEIVE,OnAudioReceive)
	

	// Video menu handlers
	ON_COMMAND(VIDEO_SEND,OnVideoSend)
	ON_COMMAND(VIDEO_RECEIVE,OnVideoReceive)
	

	// Help menu handlers
	ON_COMMAND(HELP_ABOUT,OnAbout)

	ON_COMMAND(IDC_BUTTON1,OnSendMesg)
	ON_COMMAND(IDC_STARTSERVER,OnStartServer)
	ON_COMMAND(IDC_OPENTV,OnOpenTV)	
	ON_COMMAND(IDC_SCRAMBLE,OnScramble)
	ON_COMMAND(IDC_DESCRAMBLE,OnDescramble)

	//}}AFX_MSG_MAP
END_MESSAGE_MAP()










BOOL CVideoNetDlg::OnInitDialog()
{

	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	

	// Open log file...
	log.Open("main.log",CFile::modeCreate | CFile::modeWrite);



	// Disable the menu items...at startup
	menu=this->GetMenu();
	menu->EnableMenuItem(CONF_CONNECT,MF_ENABLED);
	menu->EnableMenuItem(CONF_DISCONNECT,MF_ENABLED);
	menu->EnableMenuItem(AUDIO_SEND,MF_GRAYED);
	menu->EnableMenuItem(AUDIO_RECEIVE,MF_GRAYED);
	menu->EnableMenuItem(VIDEO_SEND,MF_GRAYED);
	menu->EnableMenuItem(VIDEO_RECEIVE,MF_GRAYED);
	menu->EnableMenuItem(VIEW_LOCAL,MF_GRAYED);
	menu->EnableMenuItem(VIEW_REMOTE,MF_GRAYED);


	SetMenu(NULL);

	//list=(CListBox*)GetDlgItem(IDC_LIST1);
	//bsend=(CButton*)GetDlgItem(IDC_BUTTON1);

	// Disable the send button
	//bsend->EnableWindow(FALSE);

		
	// Create Recording  thread
	record=new RecordSound(this);
	record->CreateThread();

	// Create Player thread
	play=new PlaySound1(this);
	play->CreateThread();


	// Prepare for capture video from webcam...
	vidcap=new VideoCapture();
	vidcap->SetDialog(this);

	isVideo=FALSE;
	if(IDYES==AfxMessageBox("Works as a CMMB Server or CMMB Client? (choose Yes: Server, choose No: Client)", MB_YESNO))
	{
		if(vidcap->Initialize()) // CMMB Server
		{
			this->m_bmpinfo=&vidcap->m_bmpinfo;
			isVideo=TRUE;

			// Initialize the compressor

			// Initialize table for RGB to YUV conversion
			InitLookupTable();

			// Initialize the compressor
			cparams.format = CPARAM_CIF;
			InitH263Encoder(&cparams);  

			// Set up the callback function
			WriteByteFunction = OwnWriteFunction;

		}
		else
		{
			AfxMessageBox("Unable to open Video Capture Device, so can only works as client");
		}
	}

	if(isVideo==FALSE) // CMMB Client
	{
		// Setup bmpinfo structure yourself
		m_bmpinfo=new BITMAPINFO;
		m_bmpinfo->bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
		m_bmpinfo->bmiHeader.biWidth=IMAGE_WIDTH;
		m_bmpinfo->bmiHeader.biHeight=IMAGE_HEIGHT;
		m_bmpinfo->bmiHeader.biPlanes=1;
		m_bmpinfo->bmiHeader.biBitCount=24;
		m_bmpinfo->bmiHeader.biCompression=0;
		m_bmpinfo->bmiHeader.biSizeImage=0;
		m_bmpinfo->bmiHeader.biXPelsPerMeter=0;
		m_bmpinfo->bmiHeader.biYPelsPerMeter=0;
		m_bmpinfo->bmiHeader.biClrUsed=0;
		m_bmpinfo->bmiHeader.biClrImportant=0;

		// Disable menu items...
		//menu->EnableMenuItem(VIEW_LOCAL,MF_DISABLED | MF_GRAYED);
		//menu->EnableMenuItem(VIDEO_SEND,MF_DISABLED | MF_GRAYED);
	
	}
	
	
	// Capturing is not yet started...
	isCaptureStarted=FALSE;

	isViewLocalVideo=(isVideo==TRUE)?TRUE:FALSE;
	isViewRemoteVideo=(isVideo==FALSE)?TRUE:FALSE;

	isAudioSend=(isVideo==TRUE)?TRUE:FALSE;
	isAudioReceive=(isVideo==FALSE)?TRUE:FALSE;

	isVideoSend=(isVideo==TRUE)?TRUE:FALSE;
	isVideoReceive=(isVideo==FALSE)?TRUE:FALSE;

	isScramble=FALSE;
	
	// Initialize decompressor
	InitH263Decoder();
	
	
	// Adjust display windows
	CWnd *wnd,*bwnd;
	CRect rect,brect;

	if(isVideo==TRUE) // CMMB Server
	{
		// disable client windows
		wnd=this->GetDlgItem(IDC_REMOTEVIDEO);	// Video display window
   		bwnd=this->GetDlgItem(IDC_REMOTEBORDER); // Border window...
		wnd->DestroyWindow();
		bwnd->DestroyWindow();
		// disable
		wnd=this->GetDlgItem(IDC_IPSTATIC);
		wnd->DestroyWindow();
		wnd=this->GetDlgItem(IDC_IP);
		wnd->DestroyWindow();
		wnd=this->GetDlgItem(IDC_OPENTV);
		wnd->DestroyWindow();
		// disable De-Scramble button
		wnd=this->GetDlgItem(IDC_DESCRAMBLE);
		wnd->DestroyWindow();

		// For server video display window
		wnd=this->GetDlgItem(IDC_LOCALVIDEO);
   		bwnd=this->GetDlgItem(IDC_LOCALBORDER);
   		bwnd->GetWindowRect(brect);
		ScreenToClient(brect);

		local_wnd_x=brect.TopLeft().x+(brect.Width()-IMAGE_WIDTH)/2;
		local_wnd_y=brect.TopLeft().y+(brect.Height()-IMAGE_HEIGHT)/2;
		
		// Centre the local video window
		wnd->SetWindowPos(&wndTop,local_wnd_x-4,local_wnd_y-4,IMAGE_WIDTH+9,IMAGE_HEIGHT+9,SWP_SHOWWINDOW | SWP_DRAWFRAME);

		SetDlgItemText(IDC_KEY, "00000000111111112222222233333333");
	}
	else // CMMB Client
	{
		// disable client windows
		wnd=this->GetDlgItem(IDC_LOCALVIDEO);
   		bwnd=this->GetDlgItem(IDC_LOCALBORDER);
		wnd->DestroyWindow();
		bwnd->DestroyWindow();
		// disable start button
		wnd=this->GetDlgItem(IDC_STARTSERVER);
		wnd->DestroyWindow();
		// disable Scramble button
		wnd=this->GetDlgItem(IDC_SCRAMBLE);
		wnd->DestroyWindow();

		// For remote video display window
		wnd=this->GetDlgItem(IDC_REMOTEVIDEO);	// Video display window
   		bwnd=this->GetDlgItem(IDC_REMOTEBORDER); // Border window...
   		bwnd->GetWindowRect(brect);
		ScreenToClient(brect);

		remote_wnd_x=brect.TopLeft().x+(brect.Width()-IMAGE_WIDTH)/2;
		remote_wnd_y=brect.TopLeft().y+(brect.Height()-IMAGE_HEIGHT)/2;
		
		// Centre the remote video window
		wnd->SetWindowPos(&wndTop,remote_wnd_x-4,remote_wnd_y-4,IMAGE_WIDTH+9,IMAGE_HEIGHT+9,SWP_SHOWWINDOW | SWP_DRAWFRAME);

		SetDlgItemText(IDC_IP, "10.10.2.183");
		SetDlgItemText(IDC_KEY, "00000000111111112222222233333333");
	}

	

	// Get Dialog DC
	m_hdc=this->GetDC()->m_hDC;

	
	// Initialize DIB for drawing...
	hdib=::DrawDibOpen();
	if(hdib!=NULL)
	{
		::DrawDibBegin(hdib,
					   m_hdc,
					   -1,				// don't stretch
					   -1,				// don't stretch
					   &m_bmpinfo->bmiHeader,
					   IMAGE_WIDTH,         // width of image
					   IMAGE_HEIGHT,        // height of image
					   0				
					   );
	}
    

	// Setup the connect dialog box
	connectdlg=new ConnectDlg(NULL);
	connectdlg->SetParent(this);
	
	// Setup the parent for differnt datagram sockets
	dcontrol.SetParent(this);
	daudio.SetParent(this);
	dvideo.SetParent(this);
	//dtext.SetParent(this);

	// Create  control datagram socket for handling messages
	dcontrol.CreateSocket(PORT_CONTROL,TYPE_CONTROL);

	// Conference not yet started...
	isStarted=FALSE;

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CVideoNetDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

//  If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CVideoNetDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

HCURSOR CVideoNetDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CVideoNetDlg::OnCancel() 
{
	
	// If conference is going on...then 
	// it must be destroyed...
	if(isStarted)
	DestroyConference();

	// Close the control session
	dcontrol.CloseSocket();

	// End recording and player thread

	record->PostThreadMessage(WM_RECORDSOUND_ENDTHREAD,0,0);
	
	play->PostThreadMessage(WM_PLAYSOUND_ENDTHREAD,0,0);

	// Close the video capture device
	vidcap->Destroy();

		
	// Close graphics....
	if(hdib!=NULL)
	{
	::DrawDibEnd(hdib);
	::DrawDibClose(hdib);
	}
	
	// Close the encoder
	if(isVideo)
	ExitH263Encoder(&cparams);

	// close decoder ......
	ExitH263Decoder();
	

	CDialog::OnCancel();
}

/**
*	  Connect to remote host
*	  Invoked when user presses connect menu item
*	  Displays the connect dialog box
*/
void CVideoNetDlg::OnConnect()
{

	// Show the connect dialog box
	connectdlg->DoModal();

}

/**
*	  Disconnect the remote host
*	  Invoked when user presses disconnect menu item
*	  Stops the current conference
*/
void CVideoNetDlg::OnDisconnect()
{
	// Send notification to the remote host...
	dcontrol.SendControlMessage(MESG_DISCONNECT,NULL);

	DestroyConference();

}

/**
*	  Show / Hide the local video screen
*/
void CVideoNetDlg::OnViewLocal()
{

	CMenu *menu=this->GetMenu();
	
	if(isViewLocalVideo)
	{
	isViewLocalVideo=FALSE;
	menu->CheckMenuItem(VIEW_LOCAL,MF_BYCOMMAND | MF_UNCHECKED);
	}
	else
	{
	isViewLocalVideo=TRUE;
	menu->CheckMenuItem(VIEW_LOCAL,MF_BYCOMMAND | MF_CHECKED);
	}



}

/**
*	  Show / Hide the remote video screen
*/
void CVideoNetDlg::OnViewRemote()
{

	CMenu *menu=this->GetMenu();
	
	if(isViewRemoteVideo)
	{
	isViewRemoteVideo=FALSE;
	menu->CheckMenuItem(VIEW_REMOTE,MF_BYCOMMAND | MF_UNCHECKED);
	}
	else
	{
	isViewRemoteVideo=TRUE;
	menu->CheckMenuItem(VIEW_REMOTE,MF_BYCOMMAND | MF_CHECKED);
	}
}
	
/**
*	  Displays the volume control dialog box
*/
void CVideoNetDlg::OnVolumeControl()
{
	
	Volume vol(IDD_DIALOG3);

 vol.DoModal();


}

/**
*	  Enables/Disables transmission of audio
*     Invoked when user presses Audio -> Send menu item
*/
void CVideoNetDlg::OnAudioSend()
{

	if(isAudioSend)
	{
		// Stop recording audio
		record->SuspendThread();
		//>PostThreadMessage(WM_RECORDSOUND_STOPRECORDING,0,0);
		isAudioSend=FALSE;
		menu->CheckMenuItem(AUDIO_SEND,MF_BYCOMMAND | MF_UNCHECKED);

	}
	else
	{
		// Start recording audio
		record->ResumeThread();
		//record->PostThreadMessage(WM_RECORDSOUND_STARTRECORDING,0,0);
		isAudioSend=TRUE;
		menu->CheckMenuItem(AUDIO_SEND,MF_BYCOMMAND | MF_CHECKED);
	}
}

/**
*	  Enables/Disables reception of audio
*     Invoked when user presses Audio -> Receive menu item
*/
void CVideoNetDlg::OnAudioReceive()
{


	if(isAudioReceive)
	{
		isAudioReceive=FALSE;
		menu->CheckMenuItem(AUDIO_RECEIVE,MF_BYCOMMAND | MF_UNCHECKED);
	}
	else
	{
		isAudioReceive=TRUE;
		menu->CheckMenuItem(AUDIO_RECEIVE,MF_BYCOMMAND | MF_CHECKED);
	}
}

/**
*	  Enables/Disables transmission of video
*     Invoked when user presses Video-> Send menu item
*/
void CVideoNetDlg::OnVideoSend()
{

	if(isVideoSend)
	{
		isVideoSend=FALSE;
		menu->CheckMenuItem(VIDEO_SEND,MF_BYCOMMAND | MF_UNCHECKED);
	}
	else
	{
		isVideoSend=TRUE;
		menu->CheckMenuItem(VIDEO_SEND,MF_BYCOMMAND | MF_CHECKED);
	}
}

/**
*	  Enables/Disables reception of video
*     Invoked when user presses Video -> Receive menu item
*/
void CVideoNetDlg::OnVideoReceive()
{

	if(isVideoReceive)
	{
		isVideoReceive=FALSE;
		menu->CheckMenuItem(VIDEO_RECEIVE,MF_BYCOMMAND | MF_UNCHECKED);

	
	}
	else
	{
		isVideoReceive=TRUE;
		menu->CheckMenuItem(VIDEO_RECEIVE,MF_BYCOMMAND | MF_CHECKED);
	}



}

/**
*	  Display the about dialog box
*/
void CVideoNetDlg::OnAbout()
{

	CAboutDlg dlgAbout;
	dlgAbout.DoModal();
}


/**
*	  Send the message to remote host
*/
void CVideoNetDlg::OnSendMesg()
{
unsigned char data[500];

	GetDlgItemText(IDC_EDIT1,(char*)data,500);
	
	if(strlen((const char*)data)>0)
	{
	dtext.SendTextData(data,strlen((const char*)data));
	SetDlgItemText(IDC_EDIT1,"");
	}
}

/**
*	  Display mesg received from the remote host
*/
void CVideoNetDlg::DisplayMesg(char *mesg)
{

	list->AddString(mesg);
	// Highlight the currently added string
	// This will scroll the window automatically
	list->SetCurSel(list->GetCount()-1);

	
}

/**
*	  Display frame got from remote host
*/
void CVideoNetDlg::DisplayRemoteFrame(unsigned char *data,int size)
{
	int retvalue;

	// If remote view disabled then return...
	if(!isViewRemoteVideo)
	return;

	// If video reception disabled..then return..
	if(!isVideoReceive)
	return;

	retvalue=DecompressFrame(data,size,rgbdata,buffersize);

	if(!retvalue)
	{
		log.WriteString("\n Decompression failed");
		return;
	}

	log.WriteString("\n Decompression succeeded");

	::DrawDibDraw(hdib,
				  m_hdc,
				  remote_wnd_x,		// dest : left pos
				  remote_wnd_y,		// dest : top pos
				  -1,					 // don't zoom x
				  -1,					 // don't zoom y
				  &m_bmpinfo->bmiHeader,			 // bmp header info
				  rgbdata,					 // bmp data
				  0,					 // src :left
				  0,					 // src :top
				  IMAGE_WIDTH,				 // src : width
				  IMAGE_HEIGHT,				 // src : height
				  DDF_SAME_DRAW			 // use prev params....
				  );
}

/**
*
*	Send the video frame to the remote host...
*
*/
void CVideoNetDlg::SendVideo(BYTE *data,int size)
{
	CRect wndrect;
	Bits bits;		// Various count about compression


	if(vidcap->isOver==TRUE)
	return;
	
	if(data==NULL)
	return;

	// Display the captured frame...
	if(isViewLocalVideo)
	{

	::DrawDibDraw(hdib,
				  m_hdc,
				  local_wnd_x,		// dest : left pos
				  local_wnd_y,		// dest : top pos
				  -1,					 // don't zoom x
				  -1,					 // don't zoom y
				  &m_bmpinfo->bmiHeader,			 // bmp header info
				  data,					 // bmp data
				  0,					 // src :left
				  0,					 // src :top
				  IMAGE_WIDTH,				 // src : width
				  IMAGE_HEIGHT,				 // src : height
				  DDF_SAME_DRAW			 // use prev params....
				  );

	}
	
	if(isVideoSend && isStarted)
	{

		//log.WriteString("\n Converting to YUV format..");
		
		//Convert the data from rgb format to YUV format	
		ConvertRGB2YUV(IMAGE_WIDTH,IMAGE_HEIGHT,data,yuv);

		// Reset the counter
		count=0;

		//Compress the data...to h263
		cparams.format=CPARAM_CIF;
		cparams.inter = CPARAM_INTRA;  
		cparams.Q_intra = 8;
		cparams.data=yuv;		//  Data in YUV format...
		CompressFrame(&cparams, &bits);
		
		// Transmit the compressed frame
		//log.WriteString("Transmitting the frame");
		dvideo.SendVideoData(cdata,count);

	}

}


/**
*	  Start the conference ....
*
*/
void CVideoNetDlg::StartConference()
{
	if(isStarted)
	return;

	isStarted=TRUE;

//	menu->EnableMenuItem(CONF_CONNECT,MF_DISABLED | MF_GRAYED);
//	menu->EnableMenuItem(CONF_DISCONNECT,MF_ENABLED);
}

/**
*	  Destroy the current conference
*/
void CVideoNetDlg::DestroyConference()
{

	// If not started / already destroyed..then return;
	if(isStarted==FALSE)
	return;

	isStarted=FALSE;

/*
	//isViewLocalVideo=FALSE;
	isViewRemoteVideo=FALSE;


	// Enable/disable menu items
	//menu->EnableMenuItem(CONF_CONNECT,MF_ENABLED);
	//menu->EnableMenuItem(CONF_DISCONNECT,MF_DISABLED | MF_GRAYED);


	// Disable Text session
	//dtext.CloseSocket();

	// Disable the send button...
	//bsend->EnableWindow(FALSE);

	// Clear the contents of text chat windows
	//SetDlgItemText(IDC_EDIT1,"");
	//list->ResetContent();

		
	// If recording is going on ...then stop it
	if(isAudioSend)
	record->SuspendThread();
	record->PostThreadMessage(WM_RECORDSOUND_STOPRECORDING,0,0);	 

	// stop audio session
	daudio.CloseSocket();

	// stop playing audio
	play->PostThreadMessage(WM_PLAYSOUND_STOPPLAYING,0,0);

	// stop capturing video
	if(isVideo && isCaptureStarted)
	vidcap->StopCapture();	
	
	// stop video session
	dvideo.CloseSocket();

	// Update local and remote viewer..
//	CWnd *wnd=(CWnd*)this->GetDlgItem(IDC_LOCALVIDEO);
//  wnd->Invalidate(TRUE);

//	wnd=(CWnd*)this->GetDlgItem(IDC_REMOTEVIDEO);
//  wnd->Invalidate(TRUE);

	// Reset the status..
	record->ResumeThread();

	isAudioSend=TRUE;
	menu->CheckMenuItem(AUDIO_SEND,MF_BYCOMMAND | MF_CHECKED);

	isAudioReceive=TRUE;
	menu->CheckMenuItem(AUDIO_RECEIVE,MF_BYCOMMAND | MF_CHECKED);
	
	isVideoSend=TRUE;
	menu->CheckMenuItem(VIDEO_SEND,MF_BYCOMMAND | MF_CHECKED);

	isVideoReceive=TRUE;
	menu->CheckMenuItem(VIDEO_RECEIVE,MF_BYCOMMAND | MF_CHECKED);

	menu->EnableMenuItem(AUDIO_SEND,MF_GRAYED);
	menu->EnableMenuItem(AUDIO_RECEIVE,MF_GRAYED);
	
	menu->EnableMenuItem(VIDEO_SEND,MF_GRAYED);
	menu->EnableMenuItem(VIDEO_RECEIVE,MF_GRAYED);

	menu->EnableMenuItem(VIEW_LOCAL,MF_GRAYED);
	menu->EnableMenuItem(VIEW_REMOTE,MF_GRAYED);
*/
}

void CVideoNetDlg::OnStartServer()
{
	// If already started....then return
	if(isCaptureStarted)
	return;

	isViewLocalVideo=TRUE;
	isViewRemoteVideo=FALSE;

	// Start audio Session
	daudio.CreateSocket(PORT_AUDIO,TYPE_AUDIO);
	// Start video Session
	dvideo.CreateSocket(PORT_VIDEO,TYPE_VIDEO);

	// Start recording audio
	record->PostThreadMessage(WM_RECORDSOUND_STARTRECORDING,0,0);
	// Start playing audio
	play->PostThreadMessage(WM_PLAYSOUND_STARTPLAYING,0,0);

	// Start capturing video
	if(isVideo)
	{
	
		if(vidcap->StartCapture()==FALSE)
		{
			MessageBox("Unable to start the capture");
			isCaptureStarted=FALSE;
		}
		else
		{
			isCaptureStarted=TRUE;
		}

	}
}

void CVideoNetDlg::OnOpenTV()
{
	if(isStarted == FALSE)
	{
		if(GetDlgItemText(IDC_IP,ip_address,50)<=0)
		{
			AfxMessageBox("Please input correct IP address!");
		}
		else
		{
			// Start audio Session
			daudio.CreateSocket(PORT_AUDIO,TYPE_AUDIO);
			// Start video Session
			dvideo.CreateSocket(PORT_VIDEO,TYPE_VIDEO);

			// Start playing audio
			play->PostThreadMessage(WM_PLAYSOUND_STARTPLAYING,0,0);

			isViewRemoteVideo=TRUE;
			SetDlgItemText(IDC_OPENTV, "Close TV");

			dcontrol.SendControlMessage(MESG_CONNECT,ip_address);

			isStarted=TRUE;
		}
	}
	else
	{
		isViewRemoteVideo=FALSE;

		// stop playing audio
		play->PostThreadMessage(WM_PLAYSOUND_STOPPLAYING,0,0);

		// stop audio session
		daudio.CloseSocket();
		
		// stop video session
		dvideo.CloseSocket();

	//	wnd=(CWnd*)this->GetDlgItem(IDC_REMOTEVIDEO);
	//  wnd->Invalidate(TRUE);

		SetDlgItemText(IDC_OPENTV, "Open TV");

		dcontrol.SendControlMessage(MESG_DISCONNECT,ip_address);

		isStarted=FALSE;
	}


}

void CVideoNetDlg::OnScramble()
{
	if(isScramble == FALSE)
	{
		char aes_key_tmp[33];
		if(GetDlgItemText(IDC_KEY,(LPTSTR)aes_key_tmp,33)!=32)
		{
			AfxMessageBox("Please input 32 hex numbers of key!");
		}
		else
		{
			for(int i = 0; i < 16; i++)
			{
				aes_key_c[i]=((aes_key_tmp[i*2]&0x0F)<<4 | (aes_key_tmp[i*2+1]&0x0F));
			}
			aes_key_c[16]='\0';
			isScramble = TRUE;
			aes_key = (unsigned long *)aes_key_c;
			SetDlgItemText(IDC_SCRAMBLE, "Don't Scramble");
		}
	}
	else
	{
		isScramble = FALSE;
		aes_key = NULL;
		SetDlgItemText(IDC_SCRAMBLE, "Scramble");
	}
}

void CVideoNetDlg::OnDescramble()
{
	if(isScramble == FALSE)
	{
		char aes_key_tmp[33];
		if(GetDlgItemText(IDC_KEY,(LPTSTR)aes_key_tmp,33)!=32)
		{
			AfxMessageBox("Please input 32 hex numbers of key!");
		}
		else
		{
			for(int i = 0; i < 16; i++)
			{
				aes_key_c[i]=((aes_key_tmp[i*2]&0x0F)<<4 | (aes_key_tmp[i*2+1]&0x0F));
			}
			isScramble = TRUE;
			aes_key = (unsigned long *)aes_key_c;
			SetDlgItemText(IDC_DESCRAMBLE, "Don't De-Scramble");
		}
	}
	else
	{
		isScramble = FALSE;
		aes_key = NULL;
		SetDlgItemText(IDC_DESCRAMBLE, "De-Scramble");
	}
}